package com.ruoyi.teaching.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.teaching.domain.SysUserFile;
import com.ruoyi.teaching.mapper.SysUserFileMapper;
import com.ruoyi.teaching.service.SysUserFileService;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 李志鑫
 * @since 2024-06-11
 */
@Service
public class SysUserFileServiceImpl extends ServiceImpl<SysUserFileMapper, SysUserFile> implements SysUserFileService {
    @Autowired
    private  SysUserFileMapper sysUserFileMapper;

    @Override
    public AjaxResult selectFileList(Long fileId,String message) {
        Long userId = SecurityUtils.getUserId();
        QueryWrapper<SysUserFile> sysUserFileQueryWrapper = new QueryWrapper<>();
        sysUserFileQueryWrapper.eq("user_id",userId);
        if (null!=fileId&&!fileId.equals("")) {
            sysUserFileQueryWrapper.eq("top_id",fileId);
        }else {
            sysUserFileQueryWrapper.isNull("top_id");
        }
        if (null!=message&&!message.equals("")) {
            sysUserFileQueryWrapper.like("flie_name",message);
        }
        List<SysUserFile> sysUserFiles = sysUserFileMapper.selectList(sysUserFileQueryWrapper);
        return new AjaxResult(HttpStatus.SUCCESS,"操作成功",sysUserFiles);
    }

    @Override
    public AjaxResult insertUpdateFile(SysUserFile sysUserFile) {
        Long userId = SecurityUtils.getUserId();
        sysUserFile.setUserId(userId);
        if (null!=sysUserFile.getId()) {
            sysUserFile.setFileSize(null);
            Integer i = sysUserFileMapper.updateById(sysUserFile);
            if (i == 0) {
                return new AjaxResult(HttpStatus.ERROR,"失败");
            }
        }else{
            Integer insert = sysUserFileMapper.insert(sysUserFile);
            if (insert == 0) {
                return new AjaxResult(HttpStatus.ERROR,"失败");
            }
        }
        return new AjaxResult(HttpStatus.SUCCESS,"成功");
    }

    @Override
    public AjaxResult deleteFile(Long fileId) {
        Long userId = SecurityUtils.getUserId();
        QueryWrapper<SysUserFile> sysUserFileQueryWrapper = new QueryWrapper<>();
        sysUserFileQueryWrapper.eq("user_id",userId);
        sysUserFileQueryWrapper.and(w->w.eq("id",fileId).or().eq("top_id",fileId));
        Integer delete = sysUserFileMapper.delete(sysUserFileQueryWrapper);
        if (delete == 0) {
            return new AjaxResult(HttpStatus.ERROR,"失败");
        }
        return new AjaxResult(HttpStatus.SUCCESS,"成功");
    }

    @Override
    public Double selectUserSize(Long userId) {
        QueryWrapper<SysUserFile> sysUserFileQueryWrapper = new QueryWrapper<>();
        sysUserFileQueryWrapper.eq("user_id",userId);
        sysUserFileQueryWrapper.eq("type",2);
        List<SysUserFile> sysUserFiles = sysUserFileMapper.selectList(sysUserFileQueryWrapper);
        Double size =new Double(0);
        for (SysUserFile sysUserFile : sysUserFiles) {
            Double aDouble = Double.valueOf(sysUserFile.getFileSize());
            size=size+aDouble;
        }
        return size;
    }
}
